package org.jeecg.modules.parameter.biz.service.impl;


import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.parameter.api.entity.assistance.SimAssAndPoints;
import org.jeecg.modules.parameter.api.entity.assistance.SimAssistance;
import org.jeecg.modules.parameter.api.vo.assistance.SimAssJsonVo;
import org.jeecg.modules.parameter.api.vo.assistance.SimAssPointsVo;
import org.jeecg.modules.parameter.biz.mapper.SimLineAssistanceMapper;
import org.jeecg.modules.parameter.biz.service.SimLineAssistanceService;
import org.jeecg.modules.parameter.common.biz.service.impl.FzServiceImpl;
import org.jeecg.modules.parameter.common.utils.oConvertUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LSY
 */
@Slf4j
@Primary
@Service
@Transactional(rollbackFor = Exception.class)
public class SimLineAssistanceServiceImpl extends FzServiceImpl<SimLineAssistanceMapper, SimAssistance> implements SimLineAssistanceService {

    @Resource
    private SimLineAssistanceMapper mapper;

    public List<SimAssAndPoints> getAssAndPoints(String lineId) {
        return mapper.getAllAssJoin(lineId);
    }


    @Override
    public List<SimAssJsonVo> getAllAssJson() {
        List<SimAssJsonVo> simAssJsonVos = oConvertUtils.entityListToModelList(mapper.getAllAss(), SimAssJsonVo.class);
        List<SimAssPointsVo> simAssPointsVo = oConvertUtils.entityListToModelList(mapper.getAllPoint(), SimAssPointsVo.class);
        for (SimAssJsonVo ass : simAssJsonVos) {
            List<SimAssPointsVo> filteredList = simAssPointsVo.stream()
                    .filter(item -> ass.getId().equals(item.getMainAssId())).collect(Collectors.toList());
            ass.setPoints(filteredList);
        }
        return simAssJsonVos;
    }

    @Override
    public List<SimAssJsonVo> getAllAssJsonByLineId(String lineId) {
        List<SimAssJsonVo> simAssJsonVos = oConvertUtils.entityListToModelList(mapper.getAllAssByLineId(lineId), SimAssJsonVo.class);
        List<String> assIds = simAssJsonVos.stream().map(SimAssJsonVo::getId).collect(Collectors.toList());

        List<SimAssPointsVo> simAssPointsVo = oConvertUtils.entityListToModelList(mapper.getPointByAssId(assIds), SimAssPointsVo.class);
        for (SimAssJsonVo ass : simAssJsonVos) {
            if (simAssPointsVo != null) {
                List<SimAssPointsVo> filteredList = simAssPointsVo.stream()
                        .filter(item -> ass.getId().equals(item.getMainAssId())).collect(Collectors.toList());
                ass.setPoints(filteredList);
            }
        }
        return simAssJsonVos;
    }


}
